<?php
framework::plugin ( 'mysql' );
framework::plugin ( 'page' );
class user {
    public $db;
    public static $a;
    public function __construct() {
        $this->db = mysql::getInstance ();
    }
    public function getBasicUserByEmail($email) {
        if ($email) {
            $sql = "SELECT * FROM user WHERE `email` = '$email'";
            return $this->db->getRow ( $sql );
        } else {
            return false;
        }
    }
    public function getBasicUserByPhone($phone) {
        if ($phone) {
            $sql = "SELECT * FROM user WHERE `phone` = '$phone'";
            return $this->db->getRow ( $sql );
        } else {
            return false;
        }
    }
    public function getBasicUserByUsername($username) {
        if ($username) {
            $sql = "SELECT * FROM user WHERE `username` = '$username'";
            return $this->db->getRow ( $sql );
        } else {
            return false;
        }
    }
    public function getBasicUserByCode($code) {
        if ($code) {
            $sql = "SELECT * FROM user WHERE `code` = '$code'";
            return $this->db->getRow ( $sql );
        } else {
            return false;
        }
    }
    public function insertUser($data) {
        if ($data) {
            return $this->db->insert ( 'user', $data, true );
        } else {
            return false;
        }
    }
    public function getBasicUserByUid($uid) {
        if ($uid) {
            $sql = "SELECT * FROM user WHERE `uid` = '$uid'";
            return $this->db->getRow ( $sql );
        } else {
            return false;
        }
    }
    public function getDataUser($uid) {
        if ($uid) {
            $sql = "SELECT * FROM user_data WHERE `uid` = '$uid'";
            $rs = $this->db->getRow ( $sql );
            if (! $rs) {
                $rs = array ('uid' => $uid, 'exp' => 0, 'point' => 0, 'totalpoint' => 0, 'totalexpend' => 0, 'publish' => 0 );
            }
            return $rs;
        } else {
            return false;
        }
    }
    public function setLogoin($uid) {
        if ($uid) {
            $_SESSION ['islogin'] = 1;
            $binfo = $this->getBasicUserByUid ( $uid );
            $dinfo = $this->getDataUser ( $uid );
            
            unset ( $dinfo ['uid'] );
            $_SESSION ['user'] = array_merge ( $binfo, $dinfo );
        
     //print_r($_SESSION);
        }
    }
    public function saveBasicinfo($data, $uid) {
        if ($data and $uid) {
            return $this->db->updateData ( 'user', $data, " WHERE uid = '$uid'" );
        }
        return false;
    
    }
    
    public static function getPublishTypeName($type) {
        switch ($type) {
            case '1' :
                return '广告链接';
            case '2' :
                return '软件激活码';
            case '3' :
                return '软件源码';
            case '4' :
                return '创意想法';
            case '0' :
                return '其它';
        }
        return '其它';
    }
    public static function getStatusName($status, $color = null) {
        switch ($status) {
            case '1' :
                if ($color) {
                    return '<span style="color:green">审核通过</span>';
                } else {
                    return '审核通过';
                }
            case '2' :
                if ($color) {
                    return '<span style="color:#bbb">无效过期</span>';
                } else {
                    return '无效过期';
                }
            case '0' :
                if ($color) {
                    return '<span style="color:brown">待审核</span>';
                } else {
                    return '待审核';
                }
        
        }
        return '待审核';
    }
    public function getUserPublish($uid) {
        $sql = "SELECT * FROM user_publish WHERE uid = '$uid' ";
        //设置分页信息
        $page = new Page ();
        $page->uriStyle = '?';
        $page->eachNums = 2;
        $list = $page->getPage ( $sql, 0 );
        $html = $page->htmlPage ();
        // 		echo $html;
        //返回搜索结果和分页信息
        return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
    }
    public function getUserPublishInfo($id, $uid) {
        $sql = "SELECT * FROM user_publish WHERE id='$id' AND uid = '$uid' ";
        return $this->db->getRow ( $sql );
    }
    public static function getUserLevel($exp) {
        $level = floor ( $exp / 100 ) + 1;
        return $level;
    }
    public static function getNextExp($exp) {
        $level = self::getUserLevel ( $exp );
        $nextexp = ($level) * 100;
        return $nextexp;
    }
    public function getLottery($uid, $type = -1, $today = false, $page = true) {
        if ($today == true) {
            $stime = strtotime ( date ( "Y-m-d" ) . " 00:00:00" );
            $etime = strtotime ( date ( "Y-m-d" ) . " 23:59:59" );
        } else {
            $stime = strtotime ( framework::get ( 'stime' ) . " 00:00:00" );
            $etime = strtotime ( framework::get ( 'etime' ) . " 23:59:59" );
        }
        $sql = "SELECT * FROM user_award WHERE uid = $uid ";
        if ($type != - 1) {
            $sql = $sql . "  AND `type` = $type ";
        }
        $sql = $sql . " AND timeline>=$stime and  timeline<=$etime ";
        if ($page) {
            $page = new Page ();
            $page->uriStyle = '/';
            $page->eachNums = 15;
            $list = $page->getPage ( $sql, 0 );
            $html = $page->htmlPage ();
            //返回搜索结果和分页信息
            return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
        } else {
            return $this->db->getRow ( $sql );
        }
    
    }
    
    function setUserData($data) {
        $user = $_SESSION ['user'];
        $uid = $user ['uid'];
        foreach ( $data as $k => $v ) {
            if (($user [$k] + $v) < 0) {
                return false;
            }
        }
        foreach ( $data as $k => $v ) {
            $sql = "UPDATE user_data SET `$k`=`$k`+$v WHERE uid = $uid";
            $this->db->query ( $sql );
            $_SESSION ['user'] [$k] += $v;
        }
        return true;
    }
    public static function getArardtype($id) {
        switch ($id) {
            case 0 :
                return '充值奖励';
            case 1 :
                return '推荐奖励';
            case 2 :
                return '信息奖励';
            case 3 :
                return '做任务奖励';
            case 4 :
                return '签到奖励';
            case 5 :
                return '抽奖';
            case 6 :
                return '充值奖励';
        }
    }
    public function getAddress($uid) {
        $sql = "SELECT * FROM user_addr WHERE uid = $uid ORDER BY id DESC";
        return $this->db->getAll ( $sql );
    }
    public function getBank($uid) {
        $sql = "SELECT * FROM user_bank WHERE uid = $uid ORDER BY id DESC";
        return $this->db->getAll ( $sql );
    }
    public function getBankCount($uid) {
        $sql = "SELECT COUNT(1) AS c FROM user_bank WHERE uid = $uid ";
        return $this->db->getOne ( $sql );
    }
    public function getAddressCount($uid) {
        $sql = "SELECT COUNT(1) AS c FROM user_addr WHERE uid = $uid ";
        return $this->db->getOne ( $sql );
    }
    public static function dayTimeHello() {
        $h = date ( "H" ) + 0;
        if ($h >= 0 and $h < 6) {
            return '夜已深，还没睡吗？';
        }
        if ($h >= 6 and $h < 9) {
            return '早上好，吃早餐了吗？';
        }
        if ($h >= 9 and $h < 12) {
            return '上午好，做保健操了吗？别坐在位子上太久哦！';
        }
        if ($h >= 12 and $h < 13) {
            return '中午好，吃午饭了吗？';
        }
        if ($h >= 13 and $h < 18) {
            return '下午好，适当的舒展一下筋骨吧！';
        }
        if ($h >= 18 and $h < 21) {
            return '晚上好，吃晚饭了吗？';
        }
        if ($h >= 21 and $h <= 23) {
            return '晚上好，准备洗洗睡了吧！';
        }
    }
    public static function getWeekDay() {
        $w = date ( "w" );
        if ($w == 1)
            return '一';
        if ($w == 2)
            return '二';
        if ($w == 3)
            return '三';
        if ($w == 4)
            return '四';
        if ($w == 5)
            return '五';
        if ($w == 6)
            return '六';
        if ($w == 0)
            return '日';
        return $w;
    }
    
    //随机数 概率array('a'=>50.5,'b'=>48.5,'c'=>1)
    public static function pro_rand($pro) {
        $res = '';
        $max_exp = 0;
        foreach ( $pro as $key => $value ) {
            $exp = strlen ( strchr ( $value, '.' ) ) - 1;
            if ($exp > $max_exp) {
                $max_exp = $exp;
            }
        }
        $pow_exp = pow ( 10, $max_exp );
        if ($pow_exp > 1) {
            reset ( $pro );
            foreach ( $pro as $key => $value ) {
                $pro [$key] = $value * $pow_exp;
            }
        }
        $pro_sum = array_sum ( $pro );
        $rand_num = mt_rand ( 1, $pro_sum );
        reset ( $pro );
        foreach ( $pro as $key => $value ) {
            
            if ($rand_num <= $value) {
                break;
            } else {
                $rand_num -= $value;
            }
        }
        return $key;
    }
    
    public static function editor($id, $width, $height, $msg = null) {
        
        $html = '<link type="text/css" rel="stylesheet" href="' . HIGHPHP_WWW_HOST . 'resource/js/editor/skins/default.css">';
        $html .= '<script src="' . HIGHPHP_WWW_HOST . 'resource/js/editor/kindeditor-min.js" type="text/javascript"></script>';
        
        $html .= "<script>
			KE.show({
				id : '" . $id . "',
				cssPath : './index.css',
				afterCreate : function(id) {
					KE.event.ctrl(document, 13, function() {
						KE.sync(id);
						document.forms['example'].submit();
					});
					KE.event.ctrl(KE.g[id].iframeDoc, 13, function() {
						KE.sync(id);
						document.forms['example'].submit();
					});
				}
			});
		</script>";
        $html .= '<textarea id="' . $id . '" name="' . $id . '" style="width:' . $width . 'px;height:' . $height . 'px;visibility:hidden;">' . $msg . '</textarea>';
        return $html;
    }
    
    public static function flashUpload($id, $callbackID, $auto = 'false', $mu = 'false') {
        
        $html = '';
        $p = date ( "Ymd" );
        if (key_exists ( 'user', $_SESSION )) {
            $uid = $_SESSION ['user'] ['uid'];
            $p = substr ( $uid, 0, 1 ) . '/' . $uid;
        }
        $updir = HIGHPHP_WEB_DIR . "resource/upload/images/" . $p;
        if (self::$a == 0) {
            $html = '<link type="text/css" rel="stylesheet" href="' . HIGHPHP_WWW_HOST . 'resource/js/flashupload/uploadify.css">';
            $html .= '<script src="' . HIGHPHP_WWW_HOST . 'resource/js/flashupload/swfobject.js" type="text/javascript"></script>';
            $html .= '<script src="' . HIGHPHP_WWW_HOST . 'resource/js/flashupload/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>';
        }
        $html .= '<script type="text/javascript">
	
	';
        
        $a = 1;
        $html .= "$(document).ready(function() {";
        if ($mu == 'true') {
            $html .= " var mu = true;";
        } else {
            $html .= " var mu = false;";
        }
        $html .= " $('#" . $id . "').uploadify({
	    'uploader'  : '" . HIGHPHP_WEB_DIR . "resource/js/flashupload/uploadify.swf',
	    'script'    : '" . HIGHPHP_WEB_DIR . "resource/js/flashupload/uploadify.php',
	    'cancelImg' : '" . HIGHPHP_WEB_DIR . "resource/js/flashupload/cancel.png',
	    'folder'    : '" . $updir . "',
	    'auto'      : $auto,
	    'multi'       : $mu,
	    'onComplete': function(event, ID, fileObj, response, data) {
	    					var lastPath = fileObj.filePath;
	    					lastPath = lastPath.replace('" . HIGHPHP_WEB_DIR . "','');
	    					var newPath = lastPath+'.thumb'+fileObj.type;
	    					
	    					if(mu==true){
	    						var prePath = $('#" . $callbackID . "').val();
	    						newPath = newPath+';\\n'+prePath;
							}
	    					$('#" . $callbackID . "').val(newPath);
						}
	  });
	});
	</script>";
        $html .= '<div style="width:80px;float:left;padding-left:6px;padding-top:1px;"><input id="' . $id . '" name="' . $id . '" type="file" /></div>';
        if ($auto == 'false') {
            $html .= '<div style="width:50px;float:left"><input type="button" value="上 传" onclick="$(' . "'#file_upload'" . ').uploadifyUpload();"></div>';
        }
        
        return $html;
    }
}